package yygh.msn.Service.imp;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import yygh.hospital.vo.msm.MsmVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import yygh.msn.Service.MsnService;
import yygh.msn.utils.ConstantPropertiesUtils;


import java.util.HashMap;
import java.util.Map;
import java.util.Set;


@Service
public class MsnServiceImp implements MsnService {


    //Service层，实现阿里云短信验证码的发送
    @Override
    public boolean send(String phone, String code) {
        //判断手机号是否为空
        if (StringUtils.isEmpty(phone)){
            return  false;
        }
//        //整合阿里云的短信发送服务
//        //设置相关参数
//        DefaultProfile profile = DefaultProfile.getProfile(
//                ConstantPropertiesUtils.REGION_Id,
//                ConstantPropertiesUtils.ACCESS_KEY_ID,
//                ConstantPropertiesUtils.SECRECT
//        );
//
//        DefaultAcsClient client = new DefaultAcsClient(profile);
//        CommonRequest request = new CommonRequest();
//
//        request.setMethod(MethodType.POST);
//        request.setDomain("dysmsapi.aliyuncs.com");
//        request.setVersion("2021-09-28");
//        request.setAction("SendSms");
//
//        //手机号：获取前端客户传入的phone参数
//        request.putQueryParameter("PhoneNumbers", phone);
//        //签名名称：跟阿里云短信服务项目名一致
//        request.putQueryParameter("SignName", "yyghSystem");
//        //模板code：阿里云短信服务中复制
//        request.putQueryParameter("TemplateCode", "SMS_225390318");
//        //验证码  使用json格式   {"code":"123456"}
//        Map<String,Object> param = new HashMap();
//        param.put("code",code);
//        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
//        //调用方法进行短信发送
//        try {
//            CommonResponse response = client.getCommonResponse(request);
//            System.out.println(response.getData());
//            return response.getHttpResponse().isSuccess();
//        } catch (ServerException e) {
//            e.printStackTrace();
//        } catch (ClientException e) {
//            e.printStackTrace();
//        }
        // 整合容联云短信服务
        //生产环境请求地址：app.cloopen.com
        String serverIp = "app.cloopen.com";
        //请求端口
        String serverPort = "8883";
        //主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
        String accountSId = "8aaf07087c16b0a3017c2a1ba6920618"; // 需修改
        String accountToken = "c0d2700ea195408ebd33d7518eddb3f4"; // 需修改
        //请使用管理控制台中已创建应用的APPID
        String appId = "8aaf07087c16b0a3017c2a1ba776061e";  // 需修改
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);
        // 用逗号隔开手机号，官方说最多写三个手机号，似乎能实现群发的效果，但是我没做测试
        //String to = "13957757545,17829397989";
        String templateId= "1"; // 测试模板写死是1
        String[] datas = {"123456","3"}; // datas就是发过去短信中的模板，code就是服务端生成的验证码， 3表示几分钟内有效
        // 下面这些可选的都不要选，否则会出问题
        //String subAppend="1234";  //可选 扩展码，四位数字 0~9999
        //String reqId="fadfafas";  //可选 第三方自定义消息id，最大支持32位英文数字，同账号下同一自然天内不允许重复
        //HashMap<String, Object> result = sdk.sendTemplateSMS(to,templateId,datas);
        // 这里的phone就是手机号码，我这是方法入参接收的，变量名为phone
        HashMap<String, Object> result = sdk.sendTemplateSMS("15868150959",templateId,datas/*,subAppend,reqId*/);
        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息（map）
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
        }else{
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
        }
        return false;
    }


    //整合mq实现短信的发送
    @Override
    public boolean sned(MsmVo msmVo) {
        if (!StringUtils.isEmpty(msmVo.getPhone())){
            String code = (String) msmVo.getParam().get("code");
            return  this.send(msmVo.getPhone(),msmVo.getParam());
        }
        return  false;
    }

    //msnVo相关消息的发送业务
    private boolean send(String phone, Map<String,Object> param) {
        //判断手机号是否为空
        if(StringUtils.isEmpty(phone)) {
            return false;
        }
        //整合阿里云短信服务
        //设置相关参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ConstantPropertiesUtils.REGION_Id,
                        ConstantPropertiesUtils.ACCESS_KEY_ID,
                        ConstantPropertiesUtils.SECRECT);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //签名名称
        request.putQueryParameter("SignName", "我的谷粒在线教育网站");
        //模板code
        request.putQueryParameter("TemplateCode", "SMS_180051135");

        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
